package com.serial.port.player;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.serial.port.player.api.SerialPortFinder;
import com.serial.port.player.bean.ComBean;
import com.serial.port.player.common.SerialHelper;

import java.io.IOException;
import java.security.InvalidParameterException;

/**
 * Created by YanZi on 2017/9/25.
 * describe：
 * modify:
 * modify date:
 */
public class TestActivity extends AppCompatActivity {

    EditText et_text;
    EditText et_num;
    EditText et_rate;

    RadioButton radio_hex;
    RadioButton radio_string;

    private SerialControl serialControl;
    private SerialPortFinder mSerialPortFinder;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        et_text = (EditText) findViewById(R.id.et_text);
        et_num = (EditText) findViewById(R.id.et_num);
        et_rate = (EditText) findViewById(R.id.et_rate);
        radio_hex = (RadioButton) findViewById(R.id.radio_hex);
        radio_string = (RadioButton) findViewById(R.id.radio_string);


        serialControl = new SerialControl();
        //打开串口


    }


    void open(View view) {
        if (et_num.getText() != null && !"".equals(et_num.getText().toString().trim())
                && et_rate.getText() != null && !"".equals(et_rate.getText().toString().trim())
                ) {
            Toast.makeText(this, "" + et_num.getText().toString(), Toast.LENGTH_SHORT).show();
        }
        OpenComPort(serialControl);
    }

    void close(View view) {
        closeComPort(serialControl);
    }

    void send(View view) {
        if (et_text.getText() != null && !"".equals(et_text.getText().toString().trim())) {
            Toast.makeText(this, "" + et_text.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    }


    //串口控制类
    private class SerialControl extends SerialHelper {
        public SerialControl() {

        }

        @Override
        protected void onDataReceived(final ComBean ComRecData) {

        }
    }

    //打开串口
    private void OpenComPort(SerialHelper ComPort) {
        try {
            ComPort.open();
        } catch (SecurityException e) {
            showMessage("打开串口失败:没有串口读/写权限!");
        } catch (IOException e) {
            showMessage("打开串口失败:未知错误!");
        } catch (InvalidParameterException e) {
            showMessage("打开串口失败:参数错误!");
        }
    }

    //关闭串口
    private void closeComPort(SerialHelper ComPort) {
        if (ComPort != null) {
            ComPort.stopSend();
            ComPort.close();
        }
    }


    private void showMessage(String sMsg) {
        Toast.makeText(this, sMsg, Toast.LENGTH_SHORT).show();
    }

    //串口发送
    private void sendPortData(SerialHelper ComPort, String sOut) {
        if (ComPort != null && ComPort.isOpen()) {
            if (radio_hex.isChecked()) {
                ComPort.sendHex(sOut);
            } else {
                ComPort.sendTxt(sOut);
            }
        }
    }


}
